package com.un.ebs.businessObject.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.enums.TableTypeEnum;
import com.un.ebs.core.metadata.EnumInfo;
import com.un.ebs.core.util.PackageUtil;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.SysBusinessObject;
import com.un.ebs.sys.dto.TableDto;
import com.un.ebs.businessObject.service.DataTableService;
import com.un.ebs.sys.domain.Table;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author aikun
 * @ClassName DataTableController
 * @Description DataTableController
 * @Date 2020/1/8
 */
@RestController
@RequestMapping("/datatable")
@Api(value = "sys-datatable", description = "数据库表管理")
public class DataTableController {
    @Autowired
    DataTableService dataTableService;


    @GetMapping("list")
    @ApiOperation(value = "list", notes = "获取表列表")
    public WebResult<IPage<TableDto>> list(@RequestParam(required = false) Integer tableType,
                                           @RequestParam(required = false) String tableName,
                                           @RequestParam(required = false) String dataSourceId,
                                           @RequestParam int pageSize,
                                           @RequestParam int pageIndex) {
        return WebUtils.success(dataTableService.list(tableType, tableName, dataSourceId, pageSize, pageIndex));
    }

    @GetMapping("listView")
    @ApiOperation(value = "listView", notes = "获取表列表")
    public WebResult<IPage<SysBusinessObject>> query(@RequestParam(required = false) TableTypeEnum tableType,
                                                     @RequestParam(required = false) String tableName,
                                                     @RequestParam(required = false) String dataSourceId,
                                                     @RequestParam int pageSize,
                                                     @RequestParam int pageIndex) {
        return WebUtils.success(dataTableService.query(dataSourceId, tableType, tableName, pageSize, pageIndex));
    }

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "新增表信息")
    public WebResult add(@Valid @RequestBody Table table) {
        dataTableService.add(table);
        return WebUtils.success();
    }

    @GetMapping("createTable")
    @ApiOperation(value = "createTable", notes = "创建表")
    public WebResult createTable(@RequestParam String id) {
        dataTableService.createTable(id);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改表信息")
    public WebResult update(@Valid @RequestBody Table table) {
        dataTableService.update(table);
        return WebUtils.success();
    }

    @GetMapping("getEnums")
    @ApiOperation(value = "getEnums", notes = "获取表类型")
    public WebResult<List<EnumInfo>> getEnums() {
        return WebUtils.success(PackageUtil.getEnums(TableTypeEnum.class));
    }

    @GetMapping("getById")
    @ApiOperation(value = "getById", notes = "id获取表信息对象")
    public WebResult<TableDto> getById(@RequestParam String id) {
        return WebUtils.success(dataTableService.getById(id));
    }

    @GetMapping("delete")
    @ApiOperation(value = "delete", notes = "删除表信息")
    public WebResult delete(@RequestParam String id) {
        dataTableService.delete(id);
        return WebUtils.success();
    }

    @GetMapping("batchDel")
    @ApiOperation(value = "batchDel", notes = "批量删除表信息")
    public WebResult batchDel(@RequestParam String[] ids) {
        dataTableService.batchDel(ids);
        return WebUtils.success();
    }

    @ApiOperation("接口定义 : 同步表")
    @GetMapping(value = "synchronizationTable")
    public WebResult synchronizationTable(@RequestParam String id, @RequestParam(required = false) String[] tableNameList) {
        dataTableService.synchronizationTable(id, tableNameList);
        return WebUtils.success();
    }

    @ApiOperation("接口定义 : 同步表字段")
    @GetMapping(value = "synchronizationColumn")
    public WebResult synchronizationColumn(@RequestParam String tableId) {
        dataTableService.synchronizationColumn(tableId);
        return WebUtils.success();
    }

    @ApiOperation("接口定义 : 获取所有表")
    @GetMapping(value = "queryTabList")
    public WebResult<List<String>> queryTabList(@RequestParam String id) {
        return WebUtils.success(dataTableService.queryTabList(id));
    }
}
